package com.tianbing.adlottery.controller.user;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.web.bind.ServletRequestUtils;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.multiaction.MultiActionController;

import com.fengniu.ejb.business.IUserBusiness;
import com.fengniu.ejb.dao.IUserDao;
import com.tianbing.adlottery.controller.storeadmin.AdminMyStoreController;
import com.tianbing.adlottery.global.IConstants;
import com.tianbing.adlottery.helper.SkinUtils;
import com.tianbing.adlottery.util.UserSession;
import com.tianbing.adlottery.util.UtilDataGroup;

/**
 * MultiActionController for the image list/upload UI.
 * 
 * @author Conan Chen
 * @since 2006.12.15
 */
public class AdminMySaleController extends MultiActionController {
	Logger logger = Logger.getLogger(AdminMyStoreController.class.getName());

	private String mySaleProductListView;

	private String addProduct2MySaleSuccView;

	private String deleteProductFromMySaleSuccView;

	private IUserBusiness userBusiness;

	private IUserDao userDao;

	private SkinUtils skinUtils;

	public ModelAndView listMySaleProduct(HttpServletRequest request, HttpServletResponse response) throws Exception {

		ModelAndView mv = new ModelAndView(mySaleProductListView);
		Map<String, Object> model = mv.getModel();

		UserSession auth = skinUtils.getUserAuthorization(request, response);
		long ssid = auth.getManagedStoreSysId();
		// get site2content for this site
		Integer pgno = ServletRequestUtils.getIntParameter(request, "pgno", 0);
		Integer pgsize = ServletRequestUtils.getIntParameter(request, "pgsize", 30);
		String odby = ServletRequestUtils.getStringParameter(request, "odby", "");
		Integer[] relationIds = { IConstants.RelationID.U2C_IS_SELLER_OF.getId() };
		List user2ContentList = userDao.getListUser2ContentBy(auth.getUserSysId(), relationIds, -1, null, null, null,
				pgno, pgsize, null);
		Long totalCount = userDao.getCountUser2ContentBy(auth.getUserSysId(), relationIds, -1, null, null, null);
		List user2ContentGroupList = UtilDataGroup.makeGroups(user2ContentList, 0, 6);

		model.put("user2ContentGroupList", user2ContentGroupList);
		model = skinUtils.preparePageFooter(request, user2ContentList, totalCount, pgno, pgsize, model);

		return mv;
	}

	public ModelAndView addProduct2MySale(HttpServletRequest request, HttpServletResponse response) throws Exception {
		// process wordtext parameter
		long csid = ServletRequestUtils.getLongParameter(request, "csid", -1);
		if (csid > 0) {
			UserSession auth = skinUtils.getUserAuthorization(request, response);
			long[] csids = { csid };
			userBusiness.addUser2Contents(auth.getUserSysId(), IConstants.RelationID.U2C_IS_SELLER_OF.getId(), csids);
		}
		return new ModelAndView(addProduct2MySaleSuccView);
	}

	public ModelAndView deleteProductFromMySale(HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		// process wordtext parameter
		long csid = ServletRequestUtils.getLongParameter(request, "csid", -1);
		if (csid > -1) {
			UserSession auth = skinUtils.getUserAuthorization(request, response);
			long[] csids = { csid };
			userDao.deleteUser2Contents(auth.getUserSysId(), IConstants.RelationID.U2C_IS_SELLER_OF.getId(), csids);
		}
		return new ModelAndView(deleteProductFromMySaleSuccView);
	}

	public void setLogger(Logger logger) {
		this.logger = logger;
	}

	public void setMySaleProductListView(String mySaleProductListView) {
		this.mySaleProductListView = mySaleProductListView;
	}

	public void setAddProduct2MySaleSuccView(String addProduct2MySaleSuccView) {
		this.addProduct2MySaleSuccView = addProduct2MySaleSuccView;
	}

	public void setDeleteProductFromMySaleSuccView(String deleteProductFromMySaleSuccView) {
		this.deleteProductFromMySaleSuccView = deleteProductFromMySaleSuccView;
	}

	public void setUserBusiness(IUserBusiness userBusiness) {
		this.userBusiness = userBusiness;
	}

	public void setUserDao(IUserDao userDao) {
		this.userDao = userDao;
	}

	public void setSkinUtils(SkinUtils skinUtils) {
		this.skinUtils = skinUtils;
	}

}
